﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using GogTasks.Logic;

namespace GogTasks.UserControls
{
    public class SuperLabel : UserControl
    {
        #region Properties
        private Bitmap _ob;
        private Graphics _g;

        private bool _isRedrawing;
        private List<String> _lines;
        private int _lineHeight;

        public bool SuspendRendering = false;

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                Redraw(true);
            }
        }

        public bool AutoSizeHeight { get; set; }

        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
                Redraw(false);
            }
        }

        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                Redraw(true);
            }
        }

        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                Redraw(false);
            }
        }

        public int MinHeight = 64;
        #endregion

        public SuperLabel()
        {
            Resize += new EventHandler(SuperLabel_Resize);
            AutoSizeHeight = true;
        }


        public override void Refresh()
        {
            //System.Diagnostics.Debug.WriteLine("  -" + Text + " refresh");
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (_ob != null)
            {
                e.Graphics.DrawImage(_ob, 0, 0);
            }
        }

        private void Redraw(bool sizeChanged)
        {
            Redraw(sizeChanged, false);
        }

        private int _lastWidth;

        private void Redraw(bool widthChanged, bool force)
        {
            if (!force && (SuspendRendering || !Visible || Parent == null)) return;
            
            Timing.Time(0);
            _isRedrawing = true;
            if (_ob != null && widthChanged)
            {
                _ob.Dispose();
                _g.Dispose();
            }

            int lineCount = 1;
            if (Text != String.Empty &&(widthChanged || _lines == null))
            {
                _lines = Util.SplitInLines(Text, CreateGraphics(), Font, Width, out _lineHeight);
                lineCount = _lines.Count;
            }

            if (widthChanged)
            {
                if (AutoSizeHeight)
                {
                    var heightOfLines = lineCount * _lineHeight;
                    if (heightOfLines > MinHeight) Height = lineCount * _lineHeight;
                    else Height = MinHeight;
                }
                _ob = new Bitmap(Width, Height);
                _g = Graphics.FromImage(_ob);
            }

            _g.FillRectangle(new SolidBrush(BackColor), 0, 0, Width, Height);
            
            // draw fg
            if (Text != string.Empty)
            {
                //int y = lineHeight;
                int y = 0;
                var brush = new SolidBrush(ForeColor);
                foreach (string line in _lines)
                {
                    _g.DrawString(line, Font, brush, 0, y);
                    y += _lineHeight;
                }

                //System.Diagnostics.Debug.WriteLine("Drawing LH=" + _lineHeight + ", H=" + Height + ", T=" + Text);
            }

            _isRedrawing = false;
            Refresh();
            Timing.Time(6); 
            //System.Diagnostics.Debug.WriteLine("Drawing LH=" + _lineHeight + ", H=" + Height + ", T=" + Text);
            _lastWidth = Width;
        }

        /// <summary>
        /// Forces a recalculation of the Height param
        /// </summary>
        public void SetHeight()
        {
            Redraw(true, true);
        }

        void SuperLabel_Resize(object sender, EventArgs e)
        {
            if (Width == _lastWidth) return;
            if (!_isRedrawing) Redraw(true);
        }
    }
}
